﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace CH.Easy.OpenLogin
{
    public class TXWeiboOpenAPI : AbstractOpenAPI
    {
        public TXWeiboOpenAPI(AbstractOauthV2 oAuth)
            : base(oAuth)
        {

        }


        public override string GetAuthorizeUrl()
        {
            return base.oAuth.GetAuthorizeUrl();
        }

        public override AccessTokenDTO GetAccessToken(string code)
        {
            return base.oAuth.GetAccessToken(code);
        }

        public override CallBackDTO GetOpenID(string accessToken)
        {
            return base.oAuth.GetOpenID(accessToken);
        }


        public override UserDTO GetUserInfo(string format)
        {
            //每个API调用必须传的参数
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"oauth_consumer_key",base.oAuth.AppKey},
                {"openid",base.oAuth.OpenID},
                {"clientip",Common.GetIP()},
                {"oauth_version","2.a"},
                {"scope","all"},
                {"format","json"}
            };
            string url = "http://open.t.qq.com/api/user/info";
            string json = base.oAuth.OpenRequest.HttpGet(url, param.GetQueryString());
            UserDTO userDTO = new UserDTO();
            userDTO.json = json;
            var tXWeibo = new
            {
                seqid = "",
                ret = "",
                data = new
                {
                    birth_day = "",
                    birth_month = "",
                    birth_year = "",
                    name = "",
                    nick = "",
                    openid = "",
                    sex = "",
                    location = "",
                    head = ""
                }
            };
            var tXWeiboResult = JsonConvert.DeserializeAnonymousType(json, tXWeibo);
            string[] addressArry = null;
            try
            {
                addressArry = tXWeiboResult.data.location.Split(' ');
                userDTO.province = addressArry[1];
                userDTO.city = addressArry[2];

            }
            catch (Exception)
            {
                userDTO.province = "";
                userDTO.city = "";

            }


            userDTO.birth_day = tXWeiboResult.data.birth_day;
            userDTO.birth_month = tXWeiboResult.data.birth_month;
            userDTO.birth_year = tXWeiboResult.data.birth_year;
            //100px，50px，30px
            userDTO.head_big = tXWeiboResult.data.head + "/100";
            userDTO.head_medium = tXWeiboResult.data.head + "/50";
            userDTO.head_small = tXWeiboResult.data.head + "/30";
            userDTO.name = tXWeiboResult.data.name;
            userDTO.nick = tXWeiboResult.data.nick;
            userDTO.openid = tXWeiboResult.data.openid;
            //1：男； 2：女； 0：未填写。 
            //这里统一性别
            if ("2".Equals(tXWeiboResult.data.sex))
            {
                userDTO.sex = "0";//小美女
            }
            else if ("1".Equals(tXWeiboResult.data.sex))
            {
                userDTO.sex = "1";//小帅哥
            }
            else
            {
                userDTO.sex = "2";//保密
            }
            userDTO.is_succeed = true;
            userDTO.open_type = base.oAuth.OpenType;
            return userDTO;
           
        }

        public override ResultDTO AddWeibo(string format, string content, string clientip, string jing, string wei, string syncflag)
        {
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"oauth_consumer_key",base.oAuth.AppKey},
                {"openid",base.oAuth.OpenID},
                {"clientip",clientip},
                {"oauth_version","2.a"},
                {"scope","all"},
                {"content",content},
                {"jing",jing},
                {"wei",wei},
                {"syncflag",syncflag}
            };
            string url = "http://open.t.qq.com/api/t/add";
            return base.oAuth.OpenRequest.HttpPost(url, param.GetQueryString()).JsonToObject(base.oAuth.OpenType);
        }

        public override ResultDTO AddPicWeiBo(string format, string content, string clientip, string jing, string wei, string picUrl, string syncflag)
        {
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"oauth_consumer_key",base.oAuth.AppKey},
                {"openid",base.oAuth.OpenID},
                {"clientip",clientip},
                {"oauth_version","2.a"},
                {"scope","all"},
                {"content",content},
                {"jing",jing},
                {"wei",wei},
                {"syncflag",syncflag}
            };
           
            string url = "http://open.t.qq.com/api/t/add_pic";
            return base.oAuth.OpenRequest.HttpPostWithImage(url, param, picUrl).JsonToObject(base.oAuth.OpenType);
        }
    }
}
